package com.kk.framework.ui.framewest;

import java.util.HashMap;
import java.util.Map;

import org.zkoss.spring.SpringUtil;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Div;
import org.zkoss.zul.Row;
import org.zkoss.zul.Timer;
import org.zkoss.zul.Vlayout;

import com.kk.framework.session.SystemSessionManager;
import com.kk.framework.session.UserSessionObject;
import com.kk.framework.ui.SystemPageManager;
import com.kk.framework.util.Constants;

/**
 * 模块导航边栏
 * @author 杨晓康
 */
@SuppressWarnings("serial")
public class ModuleNavPanel extends Vlayout implements EventListener<Event>
{
	private SystemPageManager 	_pageManager;
	private String				_currModuleId = "MODEL_DESKTOP";
	private UserSessionObject  _gblSessObj;
	private Map<String, ModuleNavToolbarbutton> _btnMap = new HashMap<String, ModuleNavToolbarbutton>();
	
	public ModuleNavPanel()
	{
		this._pageManager = (SystemPageManager)SpringUtil.getBean("pageService");
		this._gblSessObj = SystemSessionManager.getGblUserSessionData();
		UserLoginInfoPanel usrInfoPanel = new UserLoginInfoPanel();
		this.appendChild(usrInfoPanel);
		
		Div spacing = new Div();
		spacing.setHeight("10px");
		spacing.setWidth("100%");
		spacing.setStyle("border-top:1px solid #e0e2e4;border-bottom:1px solid #acaeb0;");
		this.appendChild(spacing);
		Div plugin=new Div();
		this.appendChild(plugin);
		
		this.setSpacing("0px");
		this.loadDefaultNavButtons();

		//用于响应按钮导航真实处理事件，按钮仅用于ShowBusy和发处理消息
		this.addEventListener(Events.ON_CLICK, this); 
	}
	
	private void loadDefaultNavButtons()
	{
		this.addModuleNavButton("MODEL_DESKTOP", "系统桌面模块", "/imgs/module-gis.png", "/imgs/module-gis-sel.png", "", "com.kk.user.ui.UserList");

		Timer timer = new Timer();
		this.appendChild(timer);
		timer.setDelay(1000);
		timer.setRepeats(false);
		timer.start();
		timer.addEventListener(Events.ON_TIMER, new EventListener<Event>() {
			@Override
			public void onEvent(Event event) throws Exception
			{
				setDefaultSelModule();
			}
		});
		
	}
	
	private void setDefaultSelModule()
	{
		ModuleNavToolbarbutton btn = this._btnMap.get("MODEL_DESKTOP");
		btn.setSclass("module-nav-button-sel");
		btn.setImage(btn.getSelectIconPath());
		
		String modulePath = btn.getPagePath();
		try
		{
			Class<?> moduleClass = Class.forName(modulePath);
			Borderlayout panel = (Borderlayout)moduleClass.newInstance();
			this._pageManager.getMainContent().getCenter().appendChild(panel);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	/**
	 * 添加一个模块导航按钮
	 * @param label 按钮显示名称
	 * @param iconPath 安妮图标
	 * @param leftBar 按钮对应的左侧区域
	 * @param defaultPage 点击导航按钮后默认出现的页面
	 */ 
	public void addModuleNavButton(String moduleId, String label, String iconPath, String iconSelPath, String iconHovPath, String pagePath)
	{
		ModuleNavToolbarbutton tbb = new ModuleNavToolbarbutton(label, iconPath, iconSelPath, iconHovPath, pagePath);
		tbb.setId(moduleId);
		tbb.addEventListener(Events.ON_CLICK, this);
		this.appendChild(tbb);
		this._btnMap.put(moduleId, tbb);
	}

	public Map<String, ModuleNavToolbarbutton> getAllModuleNavButtons()
	{
		return this._btnMap;
	}
	
	public String getCurrentModuleId()
	{
		return this._currModuleId;
	}
	
	/**
	 * 跳转到相应模块
	 * @author 杨晓康
	 * @param compId 模块id
	 * @param detailId 模块中对象的Id
	 */
	public void navigateToModule(String compId)
	{	
		for(ModuleNavToolbarbutton btn : this._btnMap.values())
		{
			if (btn.getId().equals(compId)) //选中
			{
				if (!btn.getSclass().endsWith("-sel"))
				{
					this._currModuleId = compId;
					
					btn.setSclass("module-nav-button-sel");
					btn.setImage(btn.getSelectIconPath());
					
					System.out.println(btn.getPagePath());
				}
			}
			else //其他未选中
			{
				if (btn.getSclass().endsWith("-sel"))
				{
					btn.setSclass("module-nav-button");
					btn.setImage(btn.getIconPath());
				}
			}
		}
	}
	
	@Override
	public void onEvent(Event event) throws Exception 
	{
		if (event.getName().equals(Events.ON_CLICK))
		{
			Component comp = event.getTarget();
			if (comp instanceof ModuleNavToolbarbutton)
			{
				ModuleNavToolbarbutton nvTb = (ModuleNavToolbarbutton)comp;
				
				if (!nvTb.getId().equals(this._currModuleId))
				{
					Clients.showBusy(Constants.MSG_LOADING);
					Events.echoEvent(Events.ON_CLICK, this, nvTb.getId());
				}
			}
			
			if (comp == this)
			{
				Object data = event.getData();
				if (data != null)
				{
					this.navigateToModule(data.toString());
				}
			}
			return;
		}
	}
}
